<{include file="../public/header.html"}>
<div class="container-fluid">

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header"><h4>设置权限</h4></div>
            <div class="card-body">
                <form action="#!" method="post">
                    <div class="form-group">
                        <label>角色名称</label>
                        <div class="form-control-static"><{$role_name}></div>
                    </div>

                    <div class="zTreeDemoBackground left">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <input type="hidden" name="access_ids" value="">
                            <input type="hidden" name="role_id" value="<{$role_id}>" id="role_id">
                            <button class="btn btn-primary" type="button" id="sub-btn">提交</button>
                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <button class="btn btn-info" type="button" id="back">返回</button>
                        </div>
                    </div>

                </form>

            </div>
        </div>
    </div>

</div>
</div>

<link rel="stylesheet" href="/static/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="/static/js/ztree_js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/js/ztree_js/jquery.ztree.excheck.js"></script>

<script src="/backend/js/bootstrap-notify.min.js"></script>
<script type="text/javascript" src="/backend/js/lightyear.js"></script>
<SCRIPT type="text/javascript">
    var setting = {
        view: {
            selectedMulti: true
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: onCheck
        }
    };

    var zNodes =<{$auth_list}>;

    var clearFlag = false;
    function onCheck(e, treeId, treeNode) {
        count();
        if (clearFlag) {
            clearCheckedOldNodes();
        }
    }
    function clearCheckedOldNodes() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getChangeCheckedNodes();
        for (var i=0, l=nodes.length; i<l; i++) {
            nodes[i].checkedOld = nodes[i].checked;
        }
    }
    function count() {
        // var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
        //     checkCount = zTree.getCheckedNodes(true).length,
        //     nocheckCount = zTree.getCheckedNodes(false).length,
        //     changeCount = zTree.getChangeCheckedNodes().length;
        // $("#checkCount").text(checkCount);
        // $("#nocheckCount").text(nocheckCount);
        // $("#changeCount").text(changeCount);

    }
    function createTree() {
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        count();
        clearFlag = $("#last").attr("checked");
    }

    $(document).ready(function(){
        createTree();
        $("#init").bind("change", createTree);
        $("#last").bind("change", createTree);
        $("#back").click(function(){
            location.href = '/backend/adminrole/index';
        });
        $("#sub-btn").click(function(){
            var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
                nodes=treeObj.getCheckedNodes(true),
                v="";
            var arr = new Array();
            for(var i=0;i<nodes.length;i++){
                arr.push(nodes[i].id);
                $("#sub-btn").val(arr.join(','));
            }

            $.ajax({
                type:"POST",
                url:"",
                data:{
                    access_ids:$("#sub-btn").val(),
                    role_id:$('#role_id').val()
                },
                //返回数据的格式
                datatype: "json",
                //成功返回之后调用的函数
                success:function(data){
                    var res = JSON.parse(data);
                    console.log(res);
                    if(res.code == 1){
                        lightyear.notify('操作成功，页面即将自动跳转~', 'success', 2000, 'mdi mdi-emoticon-happy', 'top', 'center');
                        setTimeout(function(){
                            location.href='/backend/adminrole/index';
                        }, 2000)
                    }else{
                        alert(res.msg);
                    }
                }
            });
        })
    });

</SCRIPT>

<{include file="../public/footer.html"}>